/// 订单模型
class OrderModel {
  final int id;
  final String orderNo;
  final String status;
  final double amount;
  final ProductInfo product;
  final SellerInfo? seller;
  final VirtualContent? virtualContent;
  final DateTime createdAt;
  final DateTime? paidAt;

  OrderModel({
    required this.id,
    required this.orderNo,
    required this.status,
    required this.amount,
    required this.product,
    this.seller,
    this.virtualContent,
    required this.createdAt,
    this.paidAt,
  });

  factory OrderModel.fromJson(Map<String, dynamic> json) {
    return OrderModel(
      id: json['orderId'] ?? json['id'],
      orderNo: json['orderNo'] ?? '',
      status: json['status'] ?? 'pending',
      amount: (json['amount'] is int)
          ? (json['amount'] as int).toDouble()
          : (json['amount'] as num?)?.toDouble() ?? 0.0,
      product: ProductInfo.fromJson(json['product'] ?? {}),
      seller: json['seller'] != null
          ? SellerInfo.fromJson(json['seller'])
          : null,
      virtualContent: json['virtualContent'] != null
          ? VirtualContent.fromJson(json['virtualContent'])
          : null,
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'])
          : DateTime.now(),
      paidAt:
          json['paidAt'] != null ? DateTime.parse(json['paidAt']) : null,
    );
  }

  /// 获取状态文本
  String get statusText {
    switch (status) {
      case 'pending':
        return '待支付';
      case 'paid':
        return '已支付';
      case 'cancelled':
        return '已取消';
      case 'completed':
        return '已完成';
      default:
        return '未知状态';
    }
  }

  /// 获取状态颜色
  String get statusColor {
    switch (status) {
      case 'pending':
        return '#FF9800'; // 橙色
      case 'paid':
        return '#4CAF50'; // 绿色
      case 'cancelled':
        return '#9E9E9E'; // 灰色
      case 'completed':
        return '#2196F3'; // 蓝色
      default:
        return '#000000';
    }
  }
}

/// 商品信息
class ProductInfo {
  final int id;
  final String title;
  final double price;
  final String? coverUrl;
  final String? productType;

  ProductInfo({
    required this.id,
    required this.title,
    required this.price,
    this.coverUrl,
    this.productType,
  });

  factory ProductInfo.fromJson(Map<String, dynamic> json) {
    return ProductInfo(
      id: json['id'] ?? json['productId'] ?? 0,
      title: json['title'] ?? json['productTitle'] ?? '',
      price: (json['price'] is int)
          ? (json['price'] as int).toDouble()
          : (json['price'] as num?)?.toDouble() ?? 0.0,
      coverUrl: json['coverUrl'],
      productType: json['productType'],
    );
  }
}

/// 卖家信息
class SellerInfo {
  final int userId;
  final String nickname;
  final String? avatar;

  SellerInfo({
    required this.userId,
    required this.nickname,
    this.avatar,
  });

  factory SellerInfo.fromJson(Map<String, dynamic> json) {
    return SellerInfo(
      userId: json['userId'] ?? 0,
      nickname: json['nickname'] ?? '',
      avatar: json['avatar'],
    );
  }
}

/// 虚拟内容
class VirtualContent {
  final String? downloadUrl;
  final String? accessCode;
  final String? fileSize;
  final int? validityDays;

  VirtualContent({
    this.downloadUrl,
    this.accessCode,
    this.fileSize,
    this.validityDays,
  });

  factory VirtualContent.fromJson(Map<String, dynamic> json) {
    return VirtualContent(
      downloadUrl: json['downloadUrl'],
      accessCode: json['accessCode'],
      fileSize: json['fileSize'],
      validityDays: json['validityDays'],
    );
  }

  /// 是否有下载链接
  bool get hasDownloadUrl =>
      downloadUrl != null && downloadUrl!.isNotEmpty;

  /// 是否有提取码
  bool get hasAccessCode => accessCode != null && accessCode!.isNotEmpty;

  /// 获取有效期文本
  String get validityText {
    if (validityDays == null || validityDays == -1) {
      return '永久有效';
    }
    return '$validityDays天';
  }
}
